package cn.fl.project.utils;

import cn.cmm.base.framework.utils.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 	公共方法工具类
 * @author sunqian
 *
 */
public class CommonUtils {

    /**
     * 获取IP地址（PC端）
     * @param request
     * @return String
     */
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            //Nginx多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            return ip;
        }
        return request.getRemoteAddr();
    }

}
